package com.boomsecret.protobuf;

import com.boomsecret.protobuf.proto.PersonEntity;
import com.boomsecret.protobuf.proto.PersonEntity.PersonEntityProto;
import com.google.protobuf.InvalidProtocolBufferException;

/**
 * Created by wangjinliang on 2018/3/9.
 */
public class ProtoBufDemo {
    public static void main(String[] args) {
        PersonEntity.PersonEntityProto.Builder builder = PersonEntity.PersonEntityProto.newBuilder();
        builder.setId(100);
        builder.setName("jack");
        builder.setEmail("jack@email.com");

        PersonEntityProto personEntity = builder.build();
        System.out.println(personEntity.toString());

        byte[] bytes = personEntity.toByteArray();
        for (byte index : bytes) {
            System.out.print(index);
        }
        System.out.println();

        try {
            PersonEntityProto parsePersonEntity = PersonEntityProto.parseFrom(bytes);
            System.out.println(parsePersonEntity);
        } catch (InvalidProtocolBufferException e) {
            e.printStackTrace();
        }
    }
}
